#include <iostream>
#include <fstream>

using namespace std;

int file1()
{
    int a[10];
    ofstream outfile("f1.dat", ios::out);
    if (!outfile)
    {
        cerr << "open error!" << endl;
        exit(1);
    }
    cout << "输入10个数：" << endl;
    for (int i = 0; i < 10; i++)
    {
        cin >> a[i];
        outfile << a[i] << " ";
        cout << a[i] << " ";
    }
    cout << endl;
    outfile.close();
    return 0;
}

int file2()
{
    int a[10], max, i, order;
    ifstream infile("f1.dat", ios ::in | ios ::Nocreate);
    if (!infile)
    {
        cerr << "open error!" << endl;
        exit(1);
    }
    for (i = 0; i < 10; i++)
    {
        infile >> a[i];
        cout << a[i] << " ";
    }
    cout << endl;
    max = a[0], order = 0;

    for (i = 0; i < 10; i++)
        if (a[i] > max)
        {
            max = a[i];
            order = i;
        }
    cout << "max = " << max << endl
         << "order = " << order << endl;
    infile.close();
    return 0;
}

void save_to_file()
{
    ofstream outfile("f2.dat");
    if (!outfile)
    {
        cerr << "open error!" << endl;
        exit(1);
    }
    char c[80];
    cout << "请输入一行字符：" << endl;
    cin.getline(c, 80); //从键盘读入一行字符
    for (int i = 0; c[i] != 0; i++)
        if (c[i] >= 65 && c[i] <= 90 || c[i] >= 97 && c[i] <= 122)
        {
            outfile.put(c[i]); //将字母字符存到磁盘文件f2.dat中
            //outfile << " ";
            cout << c[i]; //在屏幕显示
        }
    cout << endl;
    outfile.close(); //关闭f2.dat;
}

void get_from_file()
{
    char ch;
    ifstream infile("f2.dat", ios::in | ios::Nocreate);
    if (!infile)
    {
        cerr << "open error!" << endl;
        exit(1);
    }
    ofstream outfile("f3.dat");
    if (!outfile)
    {
        cerr << "open error!" << endl;
        exit(1);
    }
    while (infile.get(ch)) //读取字符
    {
        if (ch >= 97 && ch <= 122)
            ch = ch - 32;
        outfile.put(ch); //将转换后大写字母存入磁盘文件f3.dat
        //outfile << " ";
        cout << ch;
    }
    cout << endl;
    infile.close();
    outfile.close();
}

void display_file(char *filename)
{
    ifstream infile(filename, ios::in | ios::Nocreate);
    if (!infile)
    {
        cerr << "open error!" << endl;
        exit(1);
    }
    char ch;
    while (infile.get(ch))
        cout.put(ch);
    cout << endl;
    infile.close();
}

int main()
{
    save_to_file();
    get_from_file();
    display_file("f3.dat");
    return 0;
}
